home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 37 / IOPROG_37.ISO / SOFT / Multilizer.exe / disk1 / data1.cab / data1 / [Group9]VCL Source Standard / ivsynchr.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-12  |  2.9 KB  |  157 lines

  1. unit IvSynchr;
  2.  
  3. {$I IVMULTI.INC}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Sysutils, Messages, Classes;
  9.  
  10. type
  11.   TIvSynchroObject = class(TObject)
  12.   public
  13.     procedure Acquire; virtual;
  14.     procedure Release; virtual;
  15.   end;
  16.  
  17.   TIvHandleObject = class(TIvSynchroObject)
  18.   private
  19.     FHandle: THandle;
  20.     FLastError: Integer;
  21.  
  22.   public
  23.     destructor Destroy; override;
  24.     property LastError: Integer read FLastError;
  25.     property Handle: THandle read FHandle;
  26.   end;
  27.  
  28.   TIvWaitResult = (ivwrSignaled, ivwrTimeout, ivwrAbandoned, ivwrError);
  29.  
  30.   TIvEvent = class(TIvHandleObject)
  31.   public
  32.     constructor Create(
  33.       EventAttributes: PSecurityAttributes;
  34.       ManualReset, InitialState: Boolean;
  35.       const Name: string);
  36.     function WaitFor(Timeout: Longint): TIvWaitResult;
  37.     procedure SetEvent;
  38.     procedure ResetEvent;
  39.   end;
  40.  
  41.   TIvSimpleEvent = class(TIvEvent)
  42.   public
  43.     constructor Create;
  44.   end;
  45.  
  46.   TIvCriticalSection = class(TIvSynchroObject)
  47.   private
  48.     FSection: TRTLCriticalSection;
  49.  
  50.   public
  51.     constructor Create;
  52.     destructor Destroy; override;
  53.     procedure Acquire; override;
  54.     procedure Release; override;
  55.     procedure Enter;
  56.     procedure Leave;
  57.   end;
  58.  
  59. implementation
  60.  
  61. { TIvSynchroObject }
  62.  
  63. procedure TIvSynchroObject.Acquire;
  64. begin
  65. end;
  66.  
  67. procedure TIvSynchroObject.Release;
  68. begin
  69. end;
  70.  
  71. { TIvHandleObject }
  72.  
  73. destructor TIvHandleObject.Destroy;
  74. begin
  75.   CloseHandle(FHandle);
  76.   inherited Destroy;
  77. end;
  78.  
  79. { TIvEvent }
  80.  
  81. constructor TIvEvent.Create(
  82.   EventAttributes: PSecurityAttributes;
  83.   ManualReset, InitialState: Boolean;
  84.   const Name: string);
  85. begin
  86.   FHandle := CreateEvent(EventAttributes, ManualReset, InitialState, PChar(Name));
  87. end;
  88.  
  89. function TIvEvent.WaitFor(Timeout: Longint): TIvWaitResult;
  90. begin
  91.   case WaitForSingleObject(Handle, Timeout) of
  92.     WAIT_ABANDONED: Result := ivwrAbandoned;
  93.     WAIT_OBJECT_0: Result := ivwrSignaled;
  94.     WAIT_TIMEOUT: Result := ivwrTimeout;
  95.     WAIT_FAILED:
  96.       begin
  97.         Result := ivwrError;
  98.         FLastError := GetLastError;
  99.       end;
  100.   else
  101.     Result := ivwrError;
  102.   end;
  103. end;
  104.  
  105. procedure TIvEvent.SetEvent;
  106. begin
  107.   Windows.SetEvent(Handle);
  108. end;
  109.  
  110. procedure TIvEvent.ResetEvent;
  111. begin
  112.   Windows.ResetEvent(Handle);
  113. end;
  114.  
  115. { TIvSimpleEvent }
  116.  
  117. constructor TIvSimpleEvent.Create;
  118. begin
  119.   FHandle := CreateEvent(nil, True, False, nil);
  120. end;
  121.  
  122. { TIvCriticalSection }
  123.  
  124. constructor TIvCriticalSection.Create;
  125. begin
  126.   inherited Create;
  127.   InitializeCriticalSection(FSection);
  128. end;
  129.  
  130. destructor TIvCriticalSection.Destroy;
  131. begin
  132.   DeleteCriticalSection(FSection);
  133.   inherited Destroy;
  134. end;
  135.  
  136. procedure TIvCriticalSection.Acquire;
  137. begin
  138.   EnterCriticalSection(FSection);
  139. end;
  140.  
  141. procedure TIvCriticalSection.Release;
  142. begin
  143.   LeaveCriticalSection(FSection);
  144. end;
  145.  
  146. procedure TIvCriticalSection.Enter;
  147. begin
  148.   Acquire;
  149. end;
  150.  
  151. procedure TIvCriticalSection.Leave;
  152. begin
  153.   Release;
  154. end;
  155.  
  156. end.
  157.